﻿
Public Module share
    Public Class protocol
        Public Shared Function [set](Of argType As toBytes)(ByVal data As nettransData,
                                                            ByVal funcname As String, _
                                                            ByVal args() As argType) As Boolean
            assert(Not data Is Nothing, "data is a nothing nettransData instance.")
            If Not data.insert(funcname) Then
                Return False
            End If
            If Not args Is Nothing Then
                Dim i As Int64
                For i = 0 To args.Length() - 1
                    If Not data.insert(args(i).tobytes()) Then
                        Return False
                    End If
                Next
            End If

            Return True
        End Function

        Public Shared Function getname(ByVal data As nettransData, ByRef funcname As String) As Boolean
            assert(Not data Is Nothing, "data is a nothing nettransData instance.")
            Return data.take(0, funcname)
        End Function

        Public Shared Function write(ByVal data As nettransData,
                                     ByVal args() As toBytes,
                                     ByVal needType As Boolean) As Boolean
            assert(Not data Is Nothing)
            Dim bt() As Byte = Nothing
            If serialize(bt, 0, Nothing, needType, args) Then
                If data.push_back(New bytes()) Then
                    data.rbegin().get().attach(bt)
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        End Function

        Public Shared Function read(ByVal data As nettransData,
                                    ByRef args() As fromBytes,
                                    ByVal t() As Type,
                                    ByVal begin As Int64) As Boolean
            Return (isemptyarray(t) AndAlso readImpl(data, args, begin)) OrElse
                   (Not isemptyarray(t) AndAlso readImpl(data, args, t, begin))
        End Function

        Private Shared Function readImpl(ByVal data As nettransData,
                                         ByRef args() As fromBytes,
                                         ByVal t() As Type,
                                         ByVal begin As Int64) As Boolean
            assert(Not data Is Nothing)
            assert(Not isemptyarray(t))
            Dim bt As bytes = Nothing
            bt = data.take(begin)
            Return deserialize(bt, t, args)
        End Function

        Private Shared Function readImpl(ByVal data As nettransData,
                                         ByRef args() As fromBytes,
                                         ByVal begin As Int64) As Boolean
            assert(Not data Is Nothing)
            Dim bt As bytes = Nothing
            bt = data.take(begin)
            Return deserialize(bt, args)
        End Function

        Public Shared Function getarguments(ByVal data As nettransData, _
                                            ByRef args() As fromBytes, _
                                            ByVal t As Type) As Boolean
            assert(Not data Is Nothing, "data is a nothing nettransData instance.")
            ReDim args(data.size() - 2)
            Dim i As Int64
            For i = 0 To data.size() - 2
                Dim bt As bytes = Nothing
                If Not data.take(i + 1, bt) Then
                    Return False
                Else
                    args(i) = alloc(t)
                    args(i).frombytes(bt)
                End If
            Next
            Return True
        End Function
    End Class

    Public Class unittest
        Public Const port As Int32 = 23413
        Public Const unittestInvokeName As String = "unittestInvoke"
    End Class

    Public Sub combineUnittest()
        If strcont(directoryName(fullpath(filesystem.thislevelPathMark)), "client", False) Then
            rcclient.unittest()
        Else
            rcserve.unittest()
        End If
    End Sub
End Module
